package com.ecut.xzh.mapper;

import com.ecut.xzh.dto.ArticlePageDTO;
import com.ecut.xzh.entity.Article;
import com.ecut.xzh.vo.ArticleDetailVO;
import com.ecut.xzh.vo.ArticleVO;
import com.github.pagehelper.Page;
import org.apache.ibatis.annotations.Insert;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Select;
import org.apache.ibatis.annotations.Update;

/**
 * @author XZH
 */
@Mapper
public interface ArticleMapper {
    /**
     * 新增文章
     *
     * @param article
     */
    @Insert("insert into article(id, title, content, create_time, update_time, student_id) " +
            "values(#{id}, #{title}, #{content}, #{createTime}, #{updateTime}, #{studentId})")
    void insert(Article article);

    /**
     * 文章分页查询
     *
     * @param articlePageDTO
     * @return
     */
    Page<ArticleVO> pageArticle(ArticlePageDTO articlePageDTO);

    /**
     * 根据id查询文章详情
     * @param id
     * @return
     */
    @Select("select s.name, liked, title, content from student s, article a " +
            "where s.id = a.student_id and a.id = #{id}")
    ArticleDetailVO getById(Long id);

    /**
     * 点赞 + 1
     * @param id
     * @return
     */
    @Update("update article set liked = liked + 1 where id = #{id}")
    Boolean plusOneById(Long id);

    /**
     * 点赞 - 1
     * @param id
     * @return
     */
    @Update("update article set liked = liked - 1 where id = #{id}")
    Boolean reduceOneById(Long id);
}
